package com.googlecode.wheres_my_stuff_okay_potato;

import java.util.Calendar;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

/**
 * Activity that holds a list of lost items. Also contains options for filtering
 * a list of items with a variety of different filters.
 */
public class LostItemListActivity extends Activity implements
		OnItemSelectedListener {
	private String filter;
	private Calendar dateComp;
	private String categoryComp;
	private boolean statusComp;
	private boolean initialised = false;
	private Fragment f;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		filter = null;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lost_item_list);

		final Global GLOBAL = (Global) getApplication();
		final Context context = LostItemListActivity.this;

		findViewById(R.id.add_lost_item_b).setOnClickListener(
				new OnClickListener() {
					public void onClick(View v) {
						Intent i = new Intent(context,
								AddLostItemActivity.class);
						startActivity(i);
					}
				});

		final Spinner filter_spinner = (Spinner) findViewById(R.id.lost_item_list_filter_spinner);
		filter_spinner.setOnItemSelectedListener(this);
		ArrayAdapter<CharSequence> filter_adapter = ArrayAdapter
				.createFromResource(context, R.array.fields_array,
						android.R.layout.simple_spinner_item);
		filter_spinner.setAdapter(filter_adapter);

		List<Item> lost_items = GLOBAL.getLostItems();

		// Take the list of item objects and convert to a list of Strings.
		List<String> item_names = new LinkedList<String>();

		for (Item item : lost_items)
			item_names.add(item.toString());

		// Now use the list of names to populate the list view:
		final ListView lost_item_lv = (ListView) findViewById(R.id.lost_item_lv);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1,
				item_names);
		lost_item_lv.setAdapter(adapter);

		final EditText name_filter_et = (EditText) findViewById(R.id.lost_item_search_name_text_field);
		final EditText loc_filter_et = (EditText) findViewById(R.id.lost_item_search_location_text_field);

		findViewById(R.id.lost_item_list_filter_b).setOnClickListener(
				new OnClickListener() {
					public void onClick(View v) {
						List<Item> items = (LostItemListActivity.this.filter != null) ? filterList()
								: GLOBAL.getLostItems();
						Set<Item> allItems = new HashSet<Item>();

						// apply filters and repopulate item list
						String name_search = name_filter_et.getText()
								.toString();
						String loc_search = loc_filter_et.getText().toString();

						if (!name_search.isEmpty()) {
							for (Item item : items) {
								if (item.getName().contains(name_search))
									allItems.add(item);
							}
						} else if (!loc_search.isEmpty()) {
							for (Item item : items) {
								if (item.getLoc().contains(loc_search))
									allItems.add(item);
							}
						} else
							for (Item item : items)
								allItems.add(item);

						List<String> item_names = new LinkedList<String>();

						for (Item item : allItems)
							item_names.add(item.toString());

						ArrayAdapter<String> adapter = new ArrayAdapter<String>(
								context, android.R.layout.simple_list_item_1,
								android.R.id.text1, item_names);
						lost_item_lv.setAdapter(adapter);
					}
				});
	}

	/**
	 * Filters the lost item list. Uses all filters applied.
	 * 
	 * @return a list of items with all filtered items hidden.
	 */
	public List<Item> filterList() {
		final Global GLOBAL = (Global) getApplication();
		List<Item> lost_items = GLOBAL.getLostItems();
		List<Item> allItems = new LinkedList<Item>();

		if (filter == null)
			for (Item i : lost_items)
				allItems.add(i);
		else if (filter.equals("Status")) {
			statusComp = ((StatusPickerFragment) f).getOpen();

			for (Item i : lost_items) {
				if (i.isOpen() == statusComp)
					allItems.add(i);
			}
		} else if (filter.equals("Category")) {
			categoryComp = ((CategoryPickerFragment) f).getCategory();

			for (Item i : lost_items) {
				if (i.getItemCategory().equals(categoryComp))
					allItems.add(i);
			}
		} else if (filter.equals("Date")) {
			dateComp = ((DatePickerFragment) f).getDate();

			for (Item i : lost_items) {
				if (i.getDateCreated().before(dateComp.getTime()))
					allItems.add(i);
			}
		}

		List<String> sublist = new LinkedList<String>();

		for (Item i : allItems)
			sublist.add(i.toString());

		// Now use the list of names to populate the list view:
		ListView lost_item_lv = (ListView) findViewById(R.id.lost_item_lv);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1,
				sublist);
		lost_item_lv.setAdapter(adapter);
		return allItems;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		if (!initialised)
			initialised = true;
		else {
			filter = parent.getItemAtPosition(pos).toString();

			if (filter.equals("Date")) {
				f = new DatePickerFragment();
				((DatePickerFragment) f).show(getFragmentManager(),
						"datePicker");
			} else if (filter.equals("Status")) {
				f = new StatusPickerFragment();
				((StatusPickerFragment) f).show(getFragmentManager(),
						"statusPicker");
			} else if (filter.equals("Category")) {
				f = new CategoryPickerFragment();
				((CategoryPickerFragment) f).show(getFragmentManager(),
						"categoryPicker");
			}
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		filter = null;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lost_item_list, menu);
		return true;
	}
}